<template>
    <div style="height: 95%;width: 100%;display:flex;justify-content:center;padding-top: 20px">
        <!--弹框DIV 设置弹框的大小的地方-->
        <div style="background: #fff;width: 700px;height: 600px;box-shadow: #D3DCE6 0px 0px 20px;">
            <div style="height:520px;border: #1C84C6;margin-top: 25px" class="scrollbar">
                <div style="height: 30px;">
                    <!--标题-->
                    <span style="font-size: 18px;margin-left: 25px;">
                        新建交付计划
                    </span>
                    <!--返回列表-->
                        <span style="margin-left: 500px;" @click="tzdz">
                            <i slot="suffix" style="font-size: 30px;color: #D3DCE6" class="el-icon-close"></i>
                        </span>
                </div>
                <!--height:91%;表单在弹框显示多长-->
                <el-scrollbar style="height:91%;padding-top: 20px;">
                    <div class="shugan"></div>
                    <span style="margin-left: 10px;font-size: 14px;">基本信息</span>
                    <br/>
                    <br/>
                    <!--表单-->
                    <!--表单-->
                    <el-form style="margin-top:5px;margin-left: 30px;" :rules="rules" size="mini" :model="jhlist" ref="jhlist">
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px;">
                                    <label slot="label" style="font-size: 13px">主题：</label><br/>
                                    <el-input v-model="jhlist.deliveryTheme"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="deliveryTime">
                                    <label slot="label" style="font-size: 13px">交付日期：</label><br/>
                                    <el-date-picker
                                            v-if="this.xg==0||this.xg==2"
                                            v-model="jhlist.deliveryTime"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions"
                                            style="width:250px">
                                    </el-date-picker>
                                    <el-date-picker
                                            v-if="this.xg==1"
                                            :disabled="true"
                                            v-model="jhlist.deliveryTime"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions"
                                            style="width:250px">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px;" prop="clientId">
                                       <label slot="label" style="font-size: 13px">客户：</label><br/>
                                    <!--客户名称-->
                                    <el-popover
                                        style="width: 700px; height: 450px;"
                                        placement="right"
                                        trigger="click"
                                        v-model="kehu">
                                    <span style="font-size: 16px;">关联客户模块</span>
                                    <br/>
                                    <el-input @keyup.enter.native="cx()" v-model="kehuinput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                        <i slot="suffix" class="el-input__icon el-icon-search" @click="cx()"></i>
                                    </el-input>
                                    <el-button type="primary" style="margin-left: 130px;">新 建</el-button>
                                    <el-table
                                            highlight-current-row
                                            border
                                            ref="kehulist"
                                            :data="kehulist">
                                        <el-table-column type="selection" width="50" align="center" property="clientId"></el-table-column>
                                        <el-table-column width="150" property="clientName" label="客户名称"></el-table-column>
                                        <el-table-column width="100" property="clientRank" label="级别"></el-table-column>
                                        <el-table-column width="150" property="clientMobile" label="手机号码"></el-table-column>
                                        <el-table-column width="200" property="clientSite" label="地址"></el-table-column>
                                    </el-table>
                                    <el-pagination
                                            style="margin-left: 350px;"
                                            layout="prev, pager, next"
                                            @current-change="handleCurrentChange"
                                            :page-size="pageSize"
                                            :current-page.sync="currPage"
                                            :total="totalSize">
                                    </el-pagination>
                                    <div style="text-align: center;width: 100%" class="dialog-footer">
                                        <el-button type="primary" @click="xzkehu">保 存</el-button>
                                        <el-button type="primary" @click="kehu = false">取 消</el-button>
                                    </div>
                                    <el-input v-if="this.xg==0" v-model="jhlist.clientId.clientName" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                </el-popover>
                                    <el-input v-if="this.xg==1||this.xg==2" :disabled="true" v-model="jhlist.clientId.clientName" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="orderId">
                                    <label slot="label" style="font-size: 13px">订单：</label><br/>
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="dindan">
                                        <span style="font-size: 16px;">关联订单模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx2()" v-model="dindaninput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx2()"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 100px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="dindanlist"
                                                :data="dindanlist">
                                            <el-table-column type="selection" width="50" align="center" property="returnedmoneyId"></el-table-column>
                                            <el-table-column width="150" property="orderTheme" label="主题"></el-table-column>
                                            <el-table-column width="170" property="orderNo" label="编号"></el-table-column>
                                            <el-table-column label="下单时间" align="center" width="150">
                                                <template slot-scope="d">
                                                    {{d.row.orderTime | formatDate}}
                                                </template>
                                            </el-table-column>
                                            <el-table-column width="100" property="orderTotal" label="总金额"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="prev, pager, next"
                                                :page-size="pageSize2"
                                                @current-change="handleCurrentChange2"
                                                :current-page.sync="currPage2"
                                                :total="totalSize2">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzdd">保 存</el-button>
                                            <el-button type="primary" @click="dindan = false">取 消</el-button>
                                        </div>
                                        <el-input  v-if="this.xg==0" v-model="jhlist.orderId.orderTheme" @focus="dakai" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1||this.xg==2" :disabled="true" v-model="jhlist.orderId.orderTheme" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px">
                                    <label slot="label" style="font-size: 13px">交付产品总金额：</label><br/>
                                    <el-input :disabled="true" v-model="jhlist.deliveryMoney"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px">
                                    <label slot="label" style="font-size: 13px">交付期次：</label><br/>
                                    <el-input-number v-if="this.xg==0||this.xg==2" style="width:250px" v-model="jhlist.deliveryIssue" controls-position="right" :min="1" :max="10"></el-input-number>
                                    <el-input-number :disabled="true" v-if="this.xg==1" style="width:250px" v-model="jhlist.deliveryIssue" controls-position="right" :min="1" :max="10"></el-input-number>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px">
                                    <label slot="label" style="font-size: 13px">负责人：</label><br/>
                                    <el-input v-model="jhlist.deliveryOwner" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item>
                                    <label slot="label" style="font-size: 13px">备注：</label><br/>
                                    <textarea style="width: 580px;" v-model="jhlist.deliveryRemark" placeholder="备注说明"></textarea>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form><br/>
                    <span style="margin-left:30px;font-size: 13px">产品</span>
                    <br/>
                    <!--添加产品按钮-->
                    <el-popover
                            style="width: 700px; height: 450px;"
                            placement="left"
                            trigger="click"
                            v-model="chanp"
                    >
                        <span style="font-size: 16px;">关联产品模块</span>
                        <br/>
                        <el-input @keyup.enter.native="cx3()" v-model="chanpinput" size="mini" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx3()"></i>
                        </el-input>
                        <el-button type="primary" size="mini" style="margin-left: 130px;">新 建</el-button>

                        <el-table
                                :data="proList"
                                ref="multipleSelection"
                                highlight-current-row
                                border>
                            <el-table-column
                                    prop="orderdetailId"
                                    type="selection"
                                    width="50"
                                    align="center">
                            </el-table-column>
                            <el-table-column
                                    prop="prodetailId.productName"
                                    label="产品名称"
                                    align="center">
                            </el-table-column>
                            <el-table-column
                                    label="产品规格"
                                    align="center">
                                <template slot-scope="s">
                                        <span v-for="pro in s.row.prodetailId.qmodeldetails" :key="pro.prodetailId">
                                            {{pro.modetContent}}
                                        </span>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="orderdetailJg"
                                    label="售价（元）"
                                    align="center">
                            </el-table-column>
                            <el-table-column
                                    prop="orderdetailJfnumber"
                                    label="数量"
                                    align="center">
                            </el-table-column>
                        </el-table>
                        <el-pagination
                                style="margin-left: 350px;"
                                layout="prev, pager, next"
                                @current-change="handleCurrentChange3"
                                :current-page="currPage3"
                                :page-size="pageSize3"
                                :total="totalSize3">
                        </el-pagination>
                        <div style="text-align: center;width: 100%" class="dialog-footer">
                            <el-button type="primary" @click="trueaddpro">保 存</el-button>
                            <el-button type="primary" @click="chanp = false">取 消</el-button>
                        </div>
                        <el-button slot="reference" size="mini" :readonly='true' style="margin-left:510px;" type="primary">添加商品</el-button>
                    </el-popover>
                    <el-table :data="jhlist.xq" style="width: 80%;margin-left: 10px;font-size:13px">
                        <el-table-column
                                align="center"
                                prop="prodetailId.productName"
                                label="产品名称"
                                width="90">
                        </el-table-column>
                        <el-table-column
                                align="center"
                                label="产品规格"
                                width="150">
                            <template slot-scope="s">
                                <span v-for="pro in s.row.prodetailId.qmodeldetails" :key="pro.prodetailId">
                                    {{pro.modetContent}}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column
                                align="center"
                                label="售价"
                                prop="deliverydeliveryJg"
                                width="90">
                        </el-table-column>
                        <el-table-column
                                align="center"
                                label="数量"
                                width="150">
                            <template slot-scope="d">
                                <el-input-number @change="discount()" size="mini" :min="1" :max="d.row.sl" v-model="d.row.deliverydeliveryNumber"></el-input-number>
                            </template>
                        </el-table-column>
                        <el-table-column
                                align="center"
                                label="交付条件">
                            <template slot-scope="d">
                                <el-input size="mini" v-model="d.row.deliverydeliveryCondition" ></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column
                                align="center"
                                label="要求">
                            <template slot-scope="d">
                                <el-input size="mini" v-model="d.row.deliverydeliveryRequire" ></el-input>
                            </template>
                        </el-table-column>
                    </el-table>
                    <br/>
                    <div style="margin-left:540px;font-size: 12px;margin-top: 10px;">
                        <span>总金额：{{this.jhlist.deliveryMoney}}</span>
                        <span>元</span>
                    </div>
                    <br/>
                </el-scrollbar>
                <!--确定按钮 设置了固定定位-->
                <div style="text-align: center;width: 80%;position: absolute;top:635px;left:230px;" class="dialog-footer">
                    <el-button  @click="tzdz">取 消</el-button>
                    &nbsp;&nbsp;
                    <el-button type="primary" @click="addbusiness('jhlist')">确 定</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {formatTimeToStr} from '../router//format.js'
    export default {
        name: "ZxinzenJiaofu",
        data() {
            var hueikuan = (rule, value, callback) => {
                if (this.jhlist.clientId.clientId === null || this.jhlist.clientId.clientId === '') {
                    callback(new Error('请先选择客户'));
                } else {
                    if(this.jhlist.orderId.orderId==0){
                        callback(new Error('请选择订单'));
                    }else {
                        callback()
                    }
                }
            };
            var kehu = (rule, value, callback) => {
                if (value.clientName === null || value.clientName === '') {
                    callback(new Error('请选择客户'));
                }else {
                    callback()
                }
            };
            return {
                rules: {
                    clientId: [
                        {validator: kehu, required: true, trigger: ['blur','change']},
                    ],
                    orderId: [
                        {validator: hueikuan, required: true, trigger: ['blur','change']},
                    ],
                    deliveryTime: [
                        {required: true, message: '请选择计划交付时间', trigger: 'change' },
                    ],
                },
                chanp:false,
                chanpinput:'',
                pageSize3:5,//产品分页页大小
                currPage3:1,//产品当前页码，默认为1
                totalSize3:0,//产品总记录数
                proList:[],//产品集合
                jhlist:
                    {
                    deliveryId: 0,
                    orderId: {
                        orderId:0,
                        clientId: null,
                        conId: null,
                        orderNo: null,
                        orderTheme: '',
                        orderTotal: null,
                        orderReturned: null,
                        orderInvoice: null,
                        orderGross: null,
                        orderTime: null,
                        orderWctime: null,
                        orderExchange: null,
                        orderReturn: null,
                        orderState: null,
                        orderRemark: null,
                        orderPeople: null,
                        orderZt: null,
                        zbartersByOrderId: null,
                        zdeliverylogsByOrderId: null,
                        zorderdetailsByOrderId: null
                    },
                    clientId: {
                        clientId: 0,
                        updateTime: null,
                        clientIndustry: null,
                        clientSource: null,
                        clientUrl: null,
                        establishPerson: null,
                        establishTime: null,
                        clientName: '',
                        clientRank: null,
                        clientPhone: null,
                        clientMobile: null,
                        clientSite: null,
                        clientRemark: null,
                        clientPeople: null,
                        clientCjpeople: null,
                        clientState: 0,
                        clientIffollowup: 0,
                        clientSheng: null,
                        clientSz: null,
                        empId: null,
                        tfyclueFormsByClueId: null,
                        tfylinkmanFormsByClientId: null,
                        zbartersByClientId: null,
                        zcontractsByClientId: null,
                        zdeliverylogsByClientId: null,
                        zinvoicesByClientId: null,
                        zordersByClientId: null
                    },
                    deliveryMoney: 0,
                    deliveryTime: '',
                    deliveryIssue: null,
                    deliveryOwner: null,
                    deliveryRemark: '',
                    deliveryTheme: '',
                    deliveryStatic: '待出库',
                    xq: []
                },
                kehu:false,//客户弹出框
                kehuinput:'',//客户弹框搜索框
                kehulist:[],//客户表格信息
                pageSize:5,//客户分页页大小
                currPage:1,//客户当前页码，默认为1
                totalSize:0,//客户总记录数
                dindan:false,//订单弹出框
                dindaninput:'',//订单弹框搜索框
                dindanlist:[],//订单表格信息
                pageSize2:5,//订单分页页大小
                currPage2:1,//订单当前页码，默认为1
                totalSize2:0,//订单总记录数
                pickerOptions: {//开票日期快捷选择方式
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                xg:0,
                ys:1,
                ydx:10,
                dz:''
            };
        },
        methods: {
            tzdz(){
                if(this.dz==='ddzy'){
                    this.$router.push({
                        path:'/dindan',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }else if(this.dz==='hhxq'){
                    this.hhdcxhhdinitData(this.hhdx.barterId)
                    this.$router.push({
                        path:'/hhxianqin',
                        query: {key:this.hhdx}
                    });
                }else if(this.dz==='htzy'){
                    this.$router.push({
                        path:'/heton',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }else if(this.dz==='hhzy'){
                    this.$router.push({
                        path:'/huanhuo',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }else {
                    this.$router.push({
                        path:'/dindan',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }
            },
            //确定按钮
            addbusiness(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        if(this.jhlist.xq.length>0){
                            this.xzfapiao();
                        }else{
                            this.$message({
                                message: '产品不能为空哦！',
                                type: 'warning'
                            });
                        }
                    }else{
                        this.$message({
                            message: '验证不通过！',
                            type: 'warning'
                        });
                        return false;
                    }
                });
            },
            cx(){
                this.initData(this.currPage);
            },
            cx2(){
                this.jihuainitData(this.currPage2);
            },
            cx3(){
                this.initProData(this.currPage3);
            },
            /*当前页方法*/
            handleCurrentChange(val) {
                this.initData(val);
            },
            /*当前页方法*/
            handleCurrentChange2(val) {
                this.jihuainitData(val);
            },
            /*当前页方法*/
            handleCurrentChange3(val) {
                this.initProData(val);
            },
            /*选择产品*/
            trueaddpro(){
                this.$refs.multipleSelection.selection.forEach((a,i)=>{
                    const htxq = {};
                    htxq.prodetailId=null;
                    htxq.deliverydeliveryNumber=null;
                    htxq.deliverydeliveryRequire=null;
                    htxq.deliverydeliveryCondition=null;
                    htxq.deliverydeliveryJg=null;
                    this.$set(htxq,'prodetailId',a.prodetailId);
                    this.$set(htxq,'deliverydeliveryNumber',1);
                    this.$set(htxq,'deliverydeliveryRequire','');
                    this.$set(htxq,'deliverydeliveryCondition','');
                    this.$set(htxq,'deliverydeliveryJg',a.orderdetailJg);
                    this.$set(htxq,'sl',a.orderdetailJfnumber);
                    htxq.condeJg=null;
                    this.jhlist.xq.splice(i,0,htxq);
                    this.jhlist.deliveryMoney =this.jhlist.deliveryMoney +a.orderdetailJg;
                });

                this.chanp=false;
            },

            /*初始化产品*/
            initProData(dd){
                this.$axios.post("http://localhost:8088/dindan/list_ddxqcp2",
                    this.qs.stringify({search3: '交付计划',search:this.jhlist.orderId.orderId, page:dd, size:this.pageSize3,search2:this.chanpinput,}))
                    .then(v=>{
                        this.proList = v.data.rows;
                        this.totalSize3=v.data.total;
                    })
                    .catch()
            },
            discount(){
                this.jhlist.deliveryMoney =0;
                this.jhlist.xq.forEach(a=>{
                    this.jhlist.deliveryMoney = this.jhlist.deliveryMoney+(a.deliverydeliveryJg*a.deliverydeliveryNumber);
                })
            },
            //根据退货单ID查询退货单详情所有产品信息
            jhxqinitData(dd){
                this.$axios.post("http://localhost:8088/jf/list_jhxq",this.qs.stringify(
                    {search:dd}))
                    .then(v=>{
                        this.jhlist.xq= v.data;
                    })
                    .catch()
            },
            //点击合同输入框就查询合同信息
            dakai() {
                this.jihuainitData(this.currPage2);
            },
            //选择订单确定按钮
            xzdd() {
                this.$refs.dindanlist.selection.forEach((pro)=>{
                    this.jhlist.orderId=pro;
                });
                this.initProData(this.currPage3)
                this.dindan=false;
                this.dindaninput='';
            },
            //订单查询方法
            jihuainitData(dd) {
                this.$axios.post("http://localhost:8088/dindan/list_dd",this.qs.stringify(
                    {search3:'交付',search:this.dindaninput,search2:this.jhlist.clientId.clientId, page:dd, size:this.pageSize2,}))
                    .then(v=>{
                        this.dindanlist = v.data.rows;
                        this.totalSize2 = v.data.total;
                    })
                    .catch()
            },
            //选择客户确定按钮
            xzkehu() {
                this.$refs.kehulist.selection.forEach((pro) => {
                    this.jhlist.clientId = pro;
                });
                this.kehu = false;
                this.kehuinput = '';
            },
            //客户查询方法
            initData(dd) {
                this.$axios.post("http://localhost:8088/dindan/kehu_list", this.qs.stringify(
                    {search: this.kehuinput, page: dd, size: this.pageSize,}))
                    .then(v => {
                        this.kehulist = v.data.rows;
                        this.totalSize = v.data.total;
                    })
                    .catch()
            },
            dzxz(){
                this.$router.push({
                    path:'/zhuye/jiaofu'
                });
            },
            xzfapiao(){
                this.print("aasdas");
                this.$axios.post("http://localhost:8088/jf/add_fapiao",this.jhlist)
                    .then(()=>{
                        if(this.xg==0||this.xg==2) {
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '新增成功!',
                            });
                        }else {
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '修改成功!',
                            });
                        }
                        this.tzdz();
                    }).catch(()=>{
                        if(this.xg==0) {
                            this.$message({
                                showClose: true,
                                message: '新增失败',
                                type: 'error'
                            });
                        }else {
                            this.$message({
                                showClose: true,
                                message: '修改失败',
                                type: 'error'
                            });
                        }
                    })
            },
        },
        created: function () {
            this.initData(this.currPage);
            this.jhlist.deliveryOwner= JSON.parse(localStorage.getItem("user")).empName;
            if(this.$route.query.key!=null){
                this.jhlist=this.$route.query.key;
                this.xg = 1;
                this.jhxqinitData(this.jhlist.deliveryId)
            }
            if(this.$route.query.dd!=null){
                this.jhlist.orderId=this.$route.query.dd;
                this.jhlist.clientId=this.$route.query.dd.clientId;
                this.print(this.jhlist.orderId.orderId)
                this.xg=2;
                this.initProData(1);
            }
            if(this.$route.query.dz!=null){
                this.dz=this.$route.query.dz;
            }
            if(this.$route.query.ys!=null){
                this.ys=this.$route.query.ys;
            }
            if(this.$route.query.ydx!=null){
                this.ydx=this.$route.query.ydx;
            }
        },
        filters: {
            formatDate: function (time) {
                if (time != null && time != "") {
                    var date = new Date(time);
                    return formatTimeToStr(date, "yyyy-MM-dd");
                } else {
                    return "";
                }
            }
        }
    }
</script>

<style scoped>
    .shugan{
        border-left-color: rgb(70, 205, 207);
        border-left-width: 1.8px;
        height: 10px;
        border-left-style: solid;
        display: inline;
        font-size: 13px;
        margin-left: 25px;
    }

    /deep/ .el-scrollbar__wrap {
        overflow: scroll;
        width: 120%;
        height: 100%;
    }

</style>